package com.wwm.makerandroid.makerandroid.Utils;


import android.content.Context;
import android.support.v7.app.AlertDialog;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;

import com.lipiao.makerandroid.R;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * 时间戳格式化工具类
 * 基于单例模式-使用饿汉式
 * 把13位时间戳转换成"yyyy-MM-dd HH:mm:ss"格式，工具类
 * DateUtil.timeStampDate(String time)
 */
public class DateUtil {

    String TAG="DateUtil";
    private static  DateUtil dateUtil=new DateUtil();
    public static DateUtil getInstance() {
        return dateUtil;
    }
    private DateUtil() {
        LogUtil.d(TAG, "验证是否单例生效,app使用过程中DateUtil只初始化一次即可");
    }

    public static String timeStampDate(String time) {
        Long timeLong = Long.parseLong(time);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");//要转换的时间格式
        Date date;
        try {
            date = sdf.parse(sdf.format(timeLong));
            return sdf.format(date);
        } catch (ParseException e) {
            return null;
        }
    }
    public static String timeStampDate2(String time) {
//支付串转日期
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");//要转换的时间格式
        Date date;
        try {
            date = sdf.parse(sdf.format(time));
            return sdf.format(date);
        } catch (ParseException e) {

            return null;
        }
    }

    //获取时间选择器的时间
    public static String getTimePickerTime(TimePicker timePicker) {
        //将小时和分钟都转为两位数 统一格式 方便传数据
        int hour = timePicker.getHour();
        String hourPrix="";
        String minutePrix="";
        if (hour < 10) {
            hourPrix="0";
        }
        int minute = timePicker.getMinute();
        if (minute < 10) {
            minutePrix = "0";
        }

        String res = hourPrix+hour + ":" +minutePrix+ minute;
        return res;
    }

    //获取日期选择器的日期
   public static  String getDatePickerTime(DatePicker datePicker) {
        //将月份转天数为两位数 统一格式 方便传数据
        int month = datePicker.getMonth()+1;
        String monthPriex ="";
        String dayPriex ="";
        if (month < 10) {
            monthPriex="0";

        }
        int day = datePicker.getDayOfMonth();
        if (day < 10) {
            dayPriex="0";
        }

        String res = monthPriex+month + "-" + dayPriex+day;
        return res;
    }


    //打开对话框 //todo 抽象成的方法
    public static void showDateDialog(View view, Button button, String title, Context mainActivity) {
        AlertDialog.Builder builder = new AlertDialog.Builder(mainActivity);
        final AlertDialog dialog = builder.create();
        View dialogView = View.inflate(mainActivity, R.layout.bg_dialog_date, null);
        DatePicker datePicker = dialogView.findViewById(R.id.data_picker);
        TimePicker timePicker = dialogView.findViewById(R.id.time_picker);
        Button cancleBt = dialogView.findViewById(R.id.tv_data_dialog_cancel);
        Button doneBt = dialogView.findViewById(R.id.tv_data_dialog_done);
        TextView tv = dialogView.findViewById(R.id.tv_pickdatetime);
        timePicker.setIs24HourView(true);
        tv.setText("请选择" + title + "时间");



        cancleBt.setOnClickListener(view1 -> dialog.dismiss());
//        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.CHINA);
        doneBt.setOnClickListener(dialogView1 -> {
//            button.setText(DatedatePicker.getDrawingTime());
            button.setText(datePicker.getYear() + "-" + getDatePickerTime(datePicker)+ " " + getTimePickerTime(timePicker));

            dialog.dismiss();
        });
        dialog.setView(dialogView);
        dialog.show();
    }


}
